package com.x.servlet;

import com.x.bean.User;
import com.x.service.user.UserService;
import com.x.service.user.UserServiceImpl;
import com.x.util.Constants;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

/**
 * @author 13925
 */
public class UserAddServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("addUser--start");
        User user = new User();
        String code = req.getParameter("code");
        String name = req.getParameter("name");
        String password = req.getParameter("password");
        int grade = Integer.parseInt(req.getParameter("grade"));
        user.setCode(code);
        user.setName(name);
        user.setPassword(password);
        user.setGrade(grade);
        UserService userService = new UserServiceImpl();
        try {
            if (userService.addUser(user)) {
                resp.sendRedirect(req.getContextPath()+"/jsp/frameAdmin.jsp");
            } else {
                req.setAttribute("error","插入失败");
                req.getRequestDispatcher("useradd.jsp").forward(req, resp);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
